/*
Created By VietRise on 20/6/2014
*/

#include "ScreenLevel.h"
#include "ScreenGameplay.h"
#include "ScreenStart.h"

ScreenLevel::~ScreenLevel()
{
}

ScreenLevel::ScreenLevel()
{
	_screenSize = CCDirector::sharedDirector()->getWinSize();

	_levelCompleted = CCUserDefault::sharedUserDefault()->getIntegerForKey("levelCompleted");
	if (_levelCompleted == 0)
	{
		_levelCompleted = 1;
		CCUserDefault::sharedUserDefault()->setIntegerForKey("levelCompleted", _levelCompleted);
		CCUserDefault::sharedUserDefault()->flush();

		for (int i = 0; i < NUMBERLEVELS; i++)
		{
			_arrayStar[i] = 0;
		}
	}
	else
	{
		for (int i = 0; i < _levelCompleted; i++)
		{
			_arrayStar[i] = CCUserDefault::sharedUserDefault()->getIntegerForKey(("rateStar" + to_string(i + 1)).c_str());
		}
	}

	createScreen();
}

CCScene* ScreenLevel::scene()
{
	CCScene* scene = CCScene::create();
	ScreenLevel* layer = new ScreenLevel();
	scene->addChild(layer);
	layer->release();
	return scene;
}

void ScreenLevel::createScreen()
{
	CCSprite* bgSprite = CCSprite::create("bg_level.png");
	bgSprite->setPosition(ccp(_screenSize.width*0.5f, _screenSize.height*0.5f));
	this->addChild(bgSprite, kBackground);

	//CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sheet_sprite.plist");

	// Create Menu
	CCMenuItemSprite* item;
	CCPoint pos;
	CCSprite* sprite1;
	CCSprite* sprite2;
	CCSprite* menuItemNormal;
	CCSprite* menuItemPress;

	CCArray* arrayItem = CCArray::createWithCapacity(NUMBERLEVELS + 1);
	CCLabelBMFont* label;

	// Create items level
	for (int i = 0; i < NUMBERLEVELS; i++)
	{
		if (i < _levelCompleted)
		{
			int rate = _arrayStar[i];
			// Create MenuItemNormal
			
			switch (rate)
			{
			case 0:
				sprite1 = CCSprite::createWithSpriteFrameName("item_star0.png");	
				sprite2 = CCSprite::createWithSpriteFrameName("item_star0.png");
				break;
			case 1:
				sprite1 = CCSprite::createWithSpriteFrameName("item_star1.png");	
				sprite2 = CCSprite::createWithSpriteFrameName("item_star1.png");
				break;
			case 2:
				sprite1 = CCSprite::createWithSpriteFrameName("item_star2.png");	
				sprite2 = CCSprite::createWithSpriteFrameName("item_star2.png");
				break;
			case 3:
				sprite1 = CCSprite::createWithSpriteFrameName("item_star3.png");	
				sprite2 = CCSprite::createWithSpriteFrameName("item_star3.png");
				break;
			default:
				break;
			}
			menuItemNormal = CCSprite::createWithSpriteFrameName("obj_hole_level.png");
			sprite1->setPosition(ccp(menuItemNormal->boundingBox().size.width / 2.0f, menuItemNormal->boundingBox().size.height / 2.0f));
			sprite1->setScale(0.5f);
			menuItemNormal->addChild(sprite1, kForeground);
			menuItemNormal->setScale(0.95f);			
			// Create MenuItemPress
			menuItemPress = CCSprite::createWithSpriteFrameName("obj_hole_level.png");	
			menuItemPress->setAnchorPoint(ccp(0.5f, 0.5f));
			sprite2->setPosition(ccp(menuItemPress->boundingBox().size.width / 2.0f, menuItemNormal->boundingBox().size.height / 2.0f));
			sprite2->setScale(0.5f);
			menuItemPress->addChild(sprite2, kForeground);			
			menuItemPress->setScale(0.8f);
		}
		else
		{
			// Create MenuItemNormal
			menuItemNormal = CCSprite::createWithSpriteFrameName("obj_hole_level.png");
			sprite1 = CCSprite::createWithSpriteFrameName("item_lock.png");
			sprite1->setPosition(ccp(menuItemNormal->boundingBox().size.width / 2.0f, menuItemNormal->boundingBox().size.height / 2.0f));
			menuItemNormal->addChild(sprite1, kForeground);
			menuItemNormal->setScale(0.95f);		
			// Create MenuItemPress
			menuItemPress = CCSprite::createWithSpriteFrameName("obj_hole_level.png");
			sprite2 = CCSprite::createWithSpriteFrameName("item_lock.png");
			sprite2->setPosition(ccp(menuItemPress->boundingBox().size.width / 2.0f, menuItemNormal->boundingBox().size.height / 2.0f));
			menuItemPress->addChild(sprite2, kForeground);
			menuItemPress->setScale(0.8f);
		}
		// Create Item
		item = CCMenuItemSprite::create(menuItemNormal, menuItemPress, this, menu_selector(ScreenLevel::selectLevel));
		pos.x = _screenSize.width*0.125f + _screenSize.width*(i%NUMBERLEVELSONROW)*0.1875f;
		pos.y = _screenSize.height*0.8f - _screenSize.height*(i / NUMBERLEVELSONROW)*0.25;
		item->setPosition(pos);
		item->setTag(i + 1);
		arrayItem->addObject(item);
		// Create Label Level
		char szName[100] = { 0 };
		sprintf(szName, "Level %i", i+1);
		label = CCLabelBMFont::create(szName, "cooperblack.fnt");
		pos.y = pos.y - 60;
		label->setPosition(pos);
		this->addChild(label, kForeground);
	}

	// Create item back
	menuItemNormal = CCSprite::createWithSpriteFrameName("btn_back_normal.png");
	menuItemPress = CCSprite::createWithSpriteFrameName("btn_back_press.png");
	item = CCMenuItemSprite::create(menuItemNormal, menuItemPress, this, menu_selector(ScreenLevel::pressBack));
	item->setAnchorPoint(ccp(0, 0));
	item->setPosition(ccp(10, 10));
	arrayItem->addObject(item);

	_mainMenu = CCMenu::createWithArray(arrayItem);
	_mainMenu->setPosition(CCPointZero);
	this->addChild(_mainMenu, kMiddleground);
}

void ScreenLevel::selectLevel(CCObject* o)
{
	int selectedLevel = ((CCSprite*)o)->getTag();
	CCLOG("select level: %i", selectedLevel);
	CCLOG("completed level: %i", _levelCompleted);
	if (selectedLevel <= _levelCompleted)
	{
		CCScene* newScene = CCTransitionMoveInR::create(0.2f, ScreenGameplay::scene(selectedLevel, _levelCompleted));
		CCDirector::sharedDirector()->replaceScene(newScene);
	}	
}

void ScreenLevel::pressBack(CCObject* o)
{
	CCScene* newScene = CCTransitionMoveInL::create(0.2f, ScreenStart::scene());
	CCDirector::sharedDirector()->replaceScene(newScene);
}